
import { _decorator, Component, Node, Mesh, utils, MeshRenderer } from 'cc';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import yyComponent from '../../Script/Common/yyComponent';
const { ccclass, property } = _decorator;

@ccclass('StartLine')
export class StartLine extends yyComponent {

    protected static meshData;
    protected static _mesh: Mesh;
    protected static get mesh(): Mesh {
        if (!this._mesh) {
            this.createMesh();
        }
        return this._mesh;
    }
    protected static createMesh() {
        this.meshData = {
            positions: [],
            normals: [],
            indices: [],
            uvs: [],
        };

        let r = GameConfig.GameRule.roadHalfWidth - 0.1;//半径
        let radianCount = 21;//圆周上的顶点数量
        let startRadian = Math.PI * 0.5 - GameConfig.GameRule.roadMaxRadian;
        let endRadian = Math.PI * 0.5 - GameConfig.GameRule.roadMinRadian;
        let stepRadian = (endRadian - startRadian) / (radianCount - 1);
        let inPI = 1 / (endRadian - startRadian);
        let startUVx = startRadian * inPI;
        let len = 1;
        let lenCount = 2;//Z轴线上的顶点数量
        let stepZ = len / (lenCount - 1);
        let inLen = 1 / len;
        //内侧表面
        for (let i1 = startRadian; i1 <= endRadian; i1 += stepRadian) {
            let s1 = Math.sin(i1);
            let c1 = Math.cos(i1);
            let x = r * c1;
            let y = -r * s1;
            let uvx = 1 - i1 * inPI + startUVx;
            for (let z1 = 0; z1 <= len; z1 += stepZ) {
                this.meshData.positions.push(x, y, -z1);
                this.meshData.normals.push(-c1, s1, 0);
                this.meshData.uvs.push(uvx, 1 - z1 * inLen);
            }
        }
        for (let rc = 0; rc < radianCount - 1; ++rc) {
            for (let z2 = 0; z2 < lenCount - 1; ++z2) {
                let i2 = rc * lenCount + z2;
                this.meshData.indices.push(i2, i2 + 1, i2 + lenCount, i2 + 1, i2 + lenCount + 1, i2 + lenCount);
            }
        }

        this._mesh = new Mesh();
        this._mesh = utils.createMesh(this.meshData);
    }

    protected initSub() {
        this.initMesh();
    }

    @property(MeshRenderer)
    protected meshRender: MeshRenderer = null;
    protected initMesh() {
        this.meshRender.mesh = StartLine.mesh;
    }

}
